# SleepMirror - A reflection of your sleep quality
# 
# sm_visual_test.rb
# TODO: Write out @answer to a file.
# 
# 
# ©2010 JohnGary, LLC.  All Rights Reserved.
# 
# 
# Changelog:
# 
# 
# 

require 'sm_panel'
require 'rubygame'

include Rubygame
include Rubygame::Events
include Rubygame::EventTriggers
include Rubygame::EventActions

DEFAULT_ASSET = "ManInTheMirror.jpg"
MAXALPHA = 255 # Maximum alpha value
ALPHASTEP = 1  # How much to dim/brighten the image
MINALPHA = 0   # Mimimum alpha value

class SM_visual_truefalse < SM_panel
  attr_accessor :screen, :image

  # A new SM_visual_truefalse adds a Rubygame::Screen to the base SM_panel class.
  # A visual test is intended to blit(draw) it's asset, an image most likely, onto
  # Screen.
  def initialize(name="", instructions="", asset=DEFAULT_ASSET)
    super(name, instructions, asset)
    @screen=nil
    # Load the image
    this_dir = File.dirname( __FILE__ )
    file = File.expand_path( @asset, this_dir ) # default file
    @image = Surface.load( file ) #In this context, the asset is expected to be an image
    @image.colorkey=@image.get_at([0,0])
    @image.alpha=255

    # Add the True and False hooks to the base class' event table
    is_false_hook = EventHook.new( :owner => nil,
      :trigger => KeyPressTrigger.new(:f),
      :action  => MethodAction.new(:capture_answer))
    is_true_hook = EventHook.new(  :owner => nil,
      :trigger => KeyPressTrigger.new(:t),
      :action  => MethodAction.new(:capture_answer))
    append_hook(is_false_hook) #Add the T/F hooks to the event table
    append_hook(is_true_hook) #Add the T/F hooks to the event table

  end
  def capture_answer(event)
      puts "Captured #{event.key}"
      @answer = event.key
  end
  
  def perform
    # Setup the Screen for this test
    @screen = Screen.new(@image.size,0,[HWSURFACE])
    @screen.title = "#{@name} - #{@asset}" #File.basename(file)
    @screen.fill(@image.colorkey) #Fill the screen with the image's background
    @image.blit( @screen, [0,0] ) #Paste the asset onto the screen
    # Setup the event queue for this test
    events=EventQueue.new
    events.enable_new_style_events
    # Loop until we get an answer or an exit.
    loop do
      events = Rubygame::Events.fetch_sdl_events
      events.each do |event|
        #@handler.handle( event )
        handle( event )
      end
      @screen.update
      sleep 0.1
    end
  end

  def exit_script
    @answer = "Not Answered" if @answer.nil?
    puts "#{@name}- answer: #{@answer}"
    super
  end
end
